package com.hcy.app.screen.music.components

import android.content.Context
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.media3.common.MediaItem
import com.hcy.app.data.dao.Song
import com.hcy.app.screen.music.view.PlayerViewModel


@Composable
fun Song_List_Screen(
    playerVM: PlayerViewModel = hiltViewModel(),
    ctx:Context = LocalContext.current.applicationContext
){
    val playerState by playerVM.playerState.collectAsState()
    val lazyListState = rememberLazyListState()
    var highlightedIndex by remember { mutableIntStateOf(playerState.currentListIndex) }
    // 首次滚动到目标行
    LaunchedEffect(Unit) {
        lazyListState.animateScrollToItem(playerState.currentListIndex)
    }

    Column(
        modifier = Modifier
            .background(Color.White)
            .heightIn(min = LocalConfiguration.current.screenHeightDp.dp * 0.4f, max = LocalConfiguration.current.screenHeightDp.dp * 0.7f)
    ) {
        // 功能按钮区
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .padding(16.dp),
            horizontalArrangement = Arrangement.SpaceBetween
        ) {
            Text(
                "当前合计("+playerState.songList.size+")首",
                color = Color(0xFF333333)
            )
            TextButton(
                onClick = {
                    playerVM.reloadMusicList(ctx)
                }
            ) {
                Text("重新扫描")
            }
        }
        // 歌曲列表
        LazyColumn {
            items(playerState.songList.size) { index ->
                val isHighlighted = index == highlightedIndex
                SongItem(
                    isHighlighted=isHighlighted,
                    song = playerState.songList[index],
                    onItemClick = {
                        highlightedIndex = index
                        playerVM.playMedia(MediaItem.Builder()
                            .setUri(playerState.songList[index].filePath)
                            .build(),playerState.songList[index].id)
                    },
                    onFavoriteClick = { /* 处理收藏 */ }
                )
            }
        }
    }
}


@Composable
fun SongItem(
    isHighlighted: Boolean,
    song: Song,
    onItemClick: () -> Unit,
    onFavoriteClick: () -> Unit
) {
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .clickable { onItemClick() }
            .background(if (isHighlighted) Color.Yellow.copy(alpha = 0.3f) else Color.Transparent)
            .padding(16.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Column(
            modifier = Modifier.weight(1f)
        ) {
            Text(
                text = song.title,
                color = if (song.isSelected) Color(0xFFFF5A5F) else Color.Black,
                fontWeight = if (song.isSelected) FontWeight.Bold else FontWeight.Normal
            )
            Text(
                text = song.artist,
                color = Color(0xFF666666),
                fontSize = 14.sp
            )
        }

        if (isHighlighted) {
            Icon(
                imageVector = Icons.Default.PlayArrow,
                contentDescription = "正在播放",
                tint = Color(0xFFFF5A5F),
                modifier = Modifier.size(24.dp)
            )
        }

        IconButton(onClick = onFavoriteClick) {
            Icon(
                imageVector = Icons.Default.Favorite,
                contentDescription = "收藏",
                tint = Color(0xFFCCCCCC),
                modifier = Modifier.size(24.dp)
            )
        }
    }
}
